package com.hunlian.makelove.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hunlian.makelove.AppApplication;
import com.hunlian.makelove.BaseFragemt;
import com.hunlian.makelove.R;
import com.hunlian.makelove.activity.DetailActivity;
import com.hunlian.makelove.adapter.ListAdapterSendInviting;
import com.hunlian.makelove.bean.Person;
import com.hunlian.makelove.bean.UserIdAndCompleteBean;
import com.hunlian.makelove.common.AppConfig;
import com.hunlian.makelove.net.NetVisitCallback;
import com.hunlian.makelove.net.ServiceInterfaceDef;
import com.hunlian.makelove.net.api.SendInvitingServiceVisit;
import com.hunlian.makelove.tools.MyToast;
import com.hunlian.makelove.view.AutoListView;
import com.hunlian.makelove.view.AutoListView.OnLoadListener;
import com.hunlian.makelove.view.AutoListView.OnRefreshListener;

import org.json.JSONObject;

import java.util.ArrayList;

public class SendInvitingFrag extends BaseFragemt implements OnRefreshListener,
OnLoadListener{
	private int mPageNumber;
	private Activity mActivity;
	private AutoListView mAutoListView;
	private int NoLoadType = AutoListView.REFRESH;
	// 当前页码
	private int mPageIndex = 1;
	private ArrayList<Person> mListPerson;
	private ListAdapterSendInviting mAdapter;
	private SendInvitingFrag mSelf;
	private ViewGroup rootView;
	private int mCurloadDataNum = 0;
	private boolean mHasMoreData = true;

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mSelf = this;
		if (rootView == null) {
			mActivity = this.getActivity();
			rootView = (ViewGroup) inflater.inflate(R.layout.frag_opposite_sex,
					null);
			ImageButton mIbBack = (ImageButton) rootView.findViewById(R.id.back);
			mIbBack.setVisibility(View.GONE);
			TextView mTvTitle = (TextView) rootView.findViewById(R.id.title);
			mTvTitle.setText("我邀请");
			
			mAutoListView = (AutoListView) rootView.findViewById(R.id.listView);
			mAutoListView.setOnRefreshListener(mSelf);
			mAutoListView.setOnLoadListener(mSelf);
			mListPerson = new ArrayList<Person>();
			mAdapter = new ListAdapterSendInviting(mActivity);
			mAdapter.setList(mListPerson);
			mAutoListView.setAdapter(mAdapter);

			mAutoListView.setResultSize(0);
			loadData();
			setListener();
		}

		ViewGroup parent = (ViewGroup) rootView.getParent();
		if (parent != null) {
			parent.removeView(rootView);
		}

		return rootView;
	}

	private void setListener(){
		mAutoListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				//System.out.println("setListener"+position);

				if(mListPerson==null)return;
				if (mListPerson.size() >= position) {
					if (position < 1)
						return;

					Intent intent = new Intent(mActivity, DetailActivity.class);
					intent.putExtra("from", "woyaoqing");
					intent.putExtra("tarId", mListPerson.get(position-1).getUserId());
					startActivity(intent);
				}
			}
		});
	}

	private void loadData() {
		mPageIndex = 1;
		NoLoadType = AutoListView.REFRESH;	
		request();
	}

	@Override
	public void onRefresh() {
		mPageIndex = 1;
		NoLoadType = AutoListView.REFRESH;	
		request();
	}

	@Override
	public void onLoad() {

		NoLoadType = AutoListView.LOAD;
		request();
	}
	
	private void request(){
		if (!AppApplication.networkAvailable) {
			MyToast.showShort("没有网络");
			mAutoListView.onRefreshComplete();
			return;
		}
		SendInvitingServiceVisit visit = new SendInvitingServiceVisit(mActivity);
		visit.setParamters(AppConfig.getInstance().getLoginedUserId(),ServiceInterfaceDef.RECORD_COUNT_PER_PAGE+"",mPageIndex+"");
		//System.out.println("我邀请请求参数：" + visit.getParamter());
		visit.getResult(new NetVisitCallback<ArrayList<Person>,Object>(mActivity){

			@Override
			public void onSuccess(ArrayList<Person> data) {
				switch (NoLoadType) {
				case AutoListView.REFRESH:
					mAutoListView.onRefreshComplete();									
					if(mListPerson==null)return;
					mListPerson.clear();	
					break;
				case AutoListView.LOAD:
					mAutoListView.onLoadComplete();
					break;
				default:
					mAutoListView.onRefreshComplete();
					if(mListPerson==null)return;
					mListPerson.clear();
					break;
				}
				if(data==null)return;
				
				mListPerson.addAll(data);
				
				mCurloadDataNum = data.size();
				if(mCurloadDataNum>0){
					mPageIndex = mPageIndex + 1;
				}
				
				mAutoListView.setResultSize(data.size());
				mAdapter.notifyDataSetChanged();

/*				//跳转到完善资料
				skipToOrganizingDataActivity(mActivity);*/
			}

			@Override
			public void onFailure(String errorEvent, String message,Object obja) {
				switch (NoLoadType) {
					case AutoListView.REFRESH:
						mAutoListView.onRefreshComplete();
						break;
					case AutoListView.LOAD:
						mAutoListView.onLoadComplete();
						break;
					default:
						mAutoListView.onRefreshComplete();
						break;
				}
				if(ServiceInterfaceDef.SERVICE_RETURN_FAIL.equals(errorEvent)){
					try {
						JSONObject obj = new JSONObject(message);
						String msg = obj.getString("message");
						String status = obj.getString("status");
						String beans = obj.getString("beans");
						Gson gson = new Gson();
						UserIdAndCompleteBean bean = gson.fromJson(beans, UserIdAndCompleteBean.class);
						if("false".equals(bean.getHasUserId())){
							Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
							//跳转到注册
							skipToRegisterActivity(mActivity);
						}
						if("false".equals(bean.getHasComplete())){
							Toast.makeText(mActivity,msg,Toast.LENGTH_SHORT).show();
							//跳转到完善资料
							skipToOrganizingDataActivity(mActivity);
						}

					}catch (Exception e){

					}

				}else {
					super.onFailure(errorEvent, message,obja);
				}
			}
			
		});
	}
}
